package com.musiklegal;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

public class CatalogueParser extends DefaultHandler 
{

	List<DataSong> catl;
	DataSong anItem;
	private boolean in_entry =false;
	private boolean in_song = false;
	private boolean in_artist = false;
	private boolean in_idring = false;
	private boolean in_album = false;
	private boolean in_alname = false;
	private boolean in_genre = false;
	private boolean in_label = false;
	private boolean in_albumid = false;
	private boolean in_filesize=false;
	private boolean in_api=false;
	private boolean in_comp=false;
	private boolean in_pub=false;

	private StringBuffer buffer;
	
	CatalogueParser()
	{
		catl = new ArrayList<DataSong>(25);
		buffer= new StringBuffer();
		
	}
	
	public void startDocument() throws SAXException 
	{
		
	}
	
	@Override
	public void endDocument() throws SAXException 
	{
		
	}

	@Override
	public void startElement(String namespaceURI, String localName,
			String qName, Attributes atts) throws SAXException {
		//Log.i("tes", localName);
		if(localName.equals("entry"))
		{
			anItem = new DataSong();
			in_entry=true;
		}
		else if (localName.equals("song")&&in_entry) {
				this.in_song = true;	
		}else if (localName.equals("artist")&&in_entry) {
				this.in_artist = true;	
		}else if (localName.equals("id_ring")&&in_entry) {
				this.in_idring = true;	
		}
		else if (localName.equals("merchant_api")&&in_entry) {
			this.in_api = true;	
		}else if (localName.equals("album_art")&&in_entry) {
				this.in_album = true;	
		}
		else if(localName.equalsIgnoreCase("album_id")&&in_entry)
		{
			in_albumid = true;
		}
		else if (localName.equals("album_name")&&in_entry) {
				this.in_alname = true;	
		}
		else if (localName.equals("composer")&&in_entry) {
			this.in_comp = true;	
		}else if (localName.equals("publisher")&&in_entry) {
			this.in_pub = true;	
		}else if (localName.equals("genre")&&in_entry) {
				this.in_genre = true;	
		}else if (localName.equals("label")&&in_entry) {
				this.in_label = true;	
		}
		else if(localName.equals("filesize")&&in_entry)
		{
			in_filesize=true;
		}
	}
	
	@Override
	public void endElement(String namespaceURI, String localName, String qName)
			throws SAXException {
		if(localName.compareToIgnoreCase("entry")==0)
		{
			if(anItem.albid==null)
			{
				String[] t = anItem.album.split("/");
				anItem.albid = t[t.length-1];
			}
			catl.add(anItem);
			
			anItem=null;
		}
		else if (localName.equals("song")) 
		{
			anItem.song = buffer.toString();
			buffer = new StringBuffer();
			this.in_song = false;	
		}else if (localName.equals("artist")) {
			
			anItem.artist = buffer.toString();
			buffer = new StringBuffer();
			this.in_artist = false;	
		}else if (localName.equals("id_ring")) {
			anItem.idring = buffer.toString();
			buffer = new StringBuffer();
				this.in_idring = false;	
		}
		else if (localName.equals("merchant_api")) {
			anItem.api = buffer.toString();
			buffer = new StringBuffer();
				this.in_api = false;	
		}else if (localName.equals("album_art")) {
			
			anItem.album = buffer.toString();
			buffer = new StringBuffer();
				this.in_album = false;	
		}
		else if(localName.equalsIgnoreCase("album_id"))
		{
			anItem.albid = buffer.toString();
			buffer = new StringBuffer();
				in_albumid= false;
		}
		else if (localName.equals("album_name")) {
			anItem.alname = buffer.toString();
			buffer = new StringBuffer();
				this.in_alname = false;	
		}else if (localName.equals("composer")) {
			anItem.composer = buffer.toString();
			buffer = new StringBuffer();
				this.in_comp = false;	
		}else if (localName.equals("publisher")) {
			anItem.pub = buffer.toString();
			buffer = new StringBuffer();
			this.in_pub = false;
		}
		else if (localName.equals("genre")&&in_entry) {
			anItem.genre= buffer.toString();
			buffer = new StringBuffer();
				this.in_genre = false;	
				
		}else if (localName.equals("label")) {
			
			anItem.label = buffer.toString();
			buffer = new StringBuffer();
			this.in_label = false;	
		}	
	}
	
	/** Gets be called on the following structure: 
	 * <tag>characters</tag> */
	@Override
    public void characters(char ch[], int start, int length) {
		
		if(this.in_song)
		{
			buffer.append(new String(ch,start,length));
    	}
		if(this.in_idring)
		{
			buffer.append(new String(ch,start,length));
    	}
		if(this.in_api)
		{
			buffer.append(new String(ch,start,length));
    	}
		if(this.in_artist)
		{
			buffer.append(new String(ch,start,length));
    	}
		if(this.in_album)
		{
			buffer.append(new String(ch,start,length));
    	}
		if(this.in_genre)
		{
			buffer.append(new String(ch,start,length));
    	}
		if(this.in_label)
		{
			buffer.append(new String(ch,start,length));
    	}
		if(in_albumid)
		{
			buffer.append(new String(ch,start,length));
		}
		if(this.in_alname)
		{
			buffer.append(new String(ch,start,length));
    	}
		if(this.in_comp)
		{
			buffer.append(new String(ch,start,length));
    	}
		if(this.in_pub)
		{
			buffer.append(new String(ch,start,length));
    	}
		
    }
}
